package com.example.asset.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.asset.common.Result;
import com.example.asset.entity.AssetInformation;
import com.example.asset.service.AssetInformationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@Tag(name = "资产信息管理")
@RestController
@RequestMapping("/information")

public class AssetInformationController{
    @Autowired
    private AssetInformationService assetInformationService;

    /**
     * 用于json格式测试
     *
     * @return
     */
    @Operation(summary = "列表，用于测试json格式的数据")
    @GetMapping
    public List<AssetInformation> findList(){
        return assetInformationService.list();
    }

    /**
     * 用于翻页
     *
     * @param pageNum
     * @param pageSize
     * @param assetNumber (defaultValue="")
     * @return
     */
    @Operation(summary = "资产详情-用于分页")
    @GetMapping("/page")
    public Result Page(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(defaultValue = "") String assetNumber, @RequestParam(required = false) String state){
        IPage<AssetInformation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AssetInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(assetNumber), AssetInformation::getAssetNumber, assetNumber);
        queryWrapper.eq(StringUtils.isNoneBlank(state), AssetInformation::getAssetState, state);
        queryWrapper.orderByDesc(AssetInformation::getAddtime);
        IPage<AssetInformation> assetInformationIPage = assetInformationService.page(page, queryWrapper);
        return Result.success(assetInformationIPage);
    }

    /**
     * 用于添加资产信息
     *
     * @param assetInformation
     * @return
     */
    @Operation(summary = "资产详情-用于添加资产信息")
    @PostMapping
    public Result save(@RequestBody AssetInformation assetInformation){
        return Result.success(assetInformationService.saveAssetInformation(assetInformation));
    }

    /**
     * 用于删除指定单个资产信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "资产详情-用于删除指定单个资产信息")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return assetInformationService.removeById(id);
    }


    /**
     * 用于批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "资产详情-用于批量删除")
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids){
        return assetInformationService.removeBatchByIds(ids);
    }


    /**
     * excel 导入
     *
     * @param file
     * @throws Exception
     */
    @Operation(summary = "资产详情-导入资产信息")
    @PostMapping("/import")
    public Boolean imp(MultipartFile file) throws Exception{
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        //@Alias解决英文表头的问题
        List<AssetInformation> list = reader.readAll(AssetInformation.class);
        System.out.println(list);
        assetInformationService.saveBatch(list);

        return true;
    }

    /**
     * 导出资产信息
     *
     * @param response
     * @throws Exception
     */
    @Operation(summary = "资产详情-导出资产信息")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception{
        // 从数据库查询出所有的数据
        List<AssetInformation> list = assetInformationService.list();

        ExcelWriter writer = ExcelUtil.getWriter(true);

        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("资产详情", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }

}